package org.jeecg.modules.cosmetic.handler;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.cosmetic.entity.TToxicologicalIecic;
import org.jeecg.modules.cosmetic.model.vo.ToxIecicVO;
import org.jeecg.modules.cosmetic.service.ITToxicologicalIecicService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service
@Slf4j
public class ToxicologicalIecicHandler {

    @Resource
    private ITToxicologicalIecicService toxicologicalIecicService;

    /**
     * 保存和更新IECIC
     * @param vo
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @DS("cosmetic-datasource")
    public Result<?> saveUpdate(ToxIecicVO vo) {
        if (CollectionUtils.isEmpty(vo.getList())) return Result.error("保存或更新的数据不能为空！");
        toxicologicalIecicService.removeBatchByIds(toxicologicalIecicService.list(new LambdaQueryWrapper<TToxicologicalIecic>().eq(TToxicologicalIecic::getToxId, vo.getToxId())));
        vo.getList().forEach(m -> {
            TToxicologicalIecic tToxicologicalIecic = new TToxicologicalIecic();
            BeanUtils.copyProperties(m, tToxicologicalIecic);
            tToxicologicalIecic.setToxId(vo.getToxId());
            toxicologicalIecicService.save(tToxicologicalIecic);
        });
        return Result.ok();
    }

    /**
     * 删除IECIC方法
     * @param id
     * @return
     */
    public boolean delete(long id) {
        return toxicologicalIecicService.removeById(id);
    }

}
